<?php


namespace app\common\library\jobs;


use addons\wanlshop\library\WanlChat\WanlChat;
use addons\ymyauexpress\library\Express;
use addons\ymyauexpress\library\Expressty;
use app\common\library\QueueApi;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\queue\Job;

class Order extends Base
{

    public function auUploadIdcard(Job $job,$data){
        if ($job->attempts() >3){
            $this->failed($job->getName(),$data);
            $job->delete();
            return;
        }
        try {
            $package =  model('app\api\model\wanlshop\Package')->get($data['id']);
            $address = model('app\api\model\wanlshop\Packageaddress')
                ->where(['package_id' => $data['id'], 'shop_id' => $data['shop_id']])
                ->order('isaddress desc')
                ->field('id,name,mobile,address,address_name,number,image_front,image_back')
                ->find();
            $front_file = ROOT_PATH.'/public'.$address['image_front'];
            $front_base64 = base64_encode(file_get_contents($front_file));
            $back_file = ROOT_PATH.'/public'.$address['image_back'];
            $back_base64 =  base64_encode(file_get_contents($back_file));
            $params = [
                "OrderIds" => [$data['express_no']],
                "ReceiverName" => $address['name'],
                "ReceiverPhone" => $address['mobile'],
                "PhotoID" => $address['number'],
                "PhotoFront" => $front_base64,
                "PhotoRear" => $back_base64,
            ];
            if($package['delivery_mode'] == 'instead'){
                $shopConfig = get_addon_config('ymyauexpress');;
            }else{
                $shopConfig = model('app\api\model\wanlshop\ShopConfig')->where('shop_id',$data['shop_id'])->find();
            }

            if($data['express_no'][0]=="G"){
                $express = new Expressty();
                if($express->upload([
                    "idcard"=> $address['number'],
                    "receiptname"=> $address['name'],
                    "receiptphone"=> $address['mobile'],
                    "expire"=> "20320520",
                    "imgfront"=> $front_base64,
                    "imgback"=> $back_base64
                ])){
                    $package->idcard_status = 1;
                    $package->save();
                }
            }else{
                $express = new Express($shopConfig['au_member_id'],$shopConfig['au_password']);
                if($express->upload($params)){
                    $package->idcard_status = 1;
                    $package->save();
                }
            }


            $job->delete();
        }catch (Exception $e){
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }


    public function auPrint(Job $job,$data){
        if ($job->attempts() >3){
            $this->failed($job->getName(),$data);
            $job->delete();
            return;
        }
        try {
            $package =  model('app\api\model\wanlshop\Package')->get($data['id']);
            if($package['delivery_mode'] == 'instead'){
                $shopConfig = get_addon_config('ymyauexpress');;
            }else{
                $shopConfig = model('app\api\model\wanlshop\ShopConfig')->where('shop_id',$data['shop_id'])->find();
            }
            $params = [
                "orderId" => $data['express_no'],
                "printMode" => 1,
                "fileType" => 1,
                "fontSize" => 0,
            ];
            $express = new Express($shopConfig['au_member_id'],$shopConfig['au_password']);
            $ret = $express->print($params);
            if($ret){
                $package->print_url = $ret;
                $package->save();
            }
            $job->delete();
        }catch (Exception $e){
            \think\Log::write('queue-' . get_class() . '-close' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }
}